﻿@model dynamic
<h2>SQL</h2>
<h6>@Html.ActionLink("查看表", "Index", new { type = "table" }) <a onclick="showDiv('')" class="btn btn-small">添加表</a></h6>
<style>
    .TSQLREsult, .TSQLREsult td, .TSQLREsult th { border: 1px solid #3893f1; border-collapse: collapse; padding: 5px; }
    .TSQLREsult{ width: 100%;}
    #myModal{width:800px !important}
</style>
@using (Html.BeginForm())
{
    var value = string.Empty;
    var type = (Request["type"] ?? string.Empty).ToLower();
    if (type == "table")
    {
        value = "select * from sqlite_master where type='table'";//所有表
    }
    @Html.TextArea("TSQL", value, new { style = "width:500px;height:300px" })
    
    <div>
        <input type="submit" name="btnExcute" value="Excute" />
    </div>
    <div>
        Result:
        <div>
            <table width="100%" class="TSQLREsult">
                @if (Model != null && Model is List<object>)
                {
                    bool head = true;
                    foreach (var c in Model)
                    {

                        var keys = c as System.Dynamic.ExpandoObject;
                        if (keys == null)
                        {
                            continue;
                        }
                        if (head)
                        {
                            @:<tr>
                            foreach (KeyValuePair<string, object> keyValuePair in keys)
                            {
                                <th>@keyValuePair.Key
                                </th>
                                head = false;
                            }
                            @:</tr>
                        }
                           
                        <tr>
                            @{
                                foreach (KeyValuePair<string, object> keyValuePair in keys)
                                {
                                    <td>
                                        @if (keyValuePair.Key == "name" && type == "table")
                                        {
                                            <a onclick="showDiv('@keyValuePair.Value')" role="button" class="btn btn-small" data-toggle="modal">@keyValuePair.Value</a>
                                        }
                                        else
                                        {
                                            <text>@keyValuePair.Value</text>
                                        }
                                    </td>

                                }
                            }
                        </tr>
                    }
                }
                else
                {
                    @Model
                }
            </table>
        </div>
    </div>
    <script type="text/javascript">
        function showDiv(table) {
            $('#myModal').removeData("modal");
            $('#myModal').modal({
                keyboard: false,
                remote: "@Url.Action("Table")?table="+table
            });
            //$('#myModal').prop("style","700px");
            $('#myModal').modal('show');
        }
        
        //function addCharges(url) {
        //    $.post('/api' + url, $("#contractData").val(), function (r) {
        //        console.log(r);
        //        ko.applyBindings(r);
        //    });
        //}
       
    </script>
    <div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
            <h3 id="myModalLabel">表信息</h3>
        </div>
        <div class="modal-body">
            <p>Loading...</p>
        </div>
        <div class="modal-footer hide">
            <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
            <button class="btn btn-primary">Save changes</button>
        </div>
    </div>
}